import requests
import json
#python 3.8
import time
import hmac
import hashlib
import base64
import urllib.parse

timestamp = str(round(time.time() * 1000))
secret = 'SEC2fc10636ece1243d3674010764b9e2ed76e9998a95b35a9f43394caaafdd91bc'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
##自定义机器人安全设置
#https://open.dingtalk.com/document/robots/customize-robot-security-settings
# print(timestamp)
# print(sign)

url = "https://oapi.dingtalk.com/robot/send?access_token=5d25b9d2c5684252842400e376e5afab4a7e42d0ed171de4ab2c4cb2fef62597"
webhook_url = f"{url}&timestamp={timestamp}&sign={sign}"

def send_dingding_message(webhook_url, message):
    headers = {'Content-Type': 'application/json'}
    data = {
        "msgtype": "text",
        "text": {
            "content": message
        }
    }
    response = requests.post(webhook_url, data=json.dumps(data), headers=headers)

    if response.status_code == 200:
        print("消息发送成功")
    else:
        print(f"消息发送失败，错误代码：{response.status_code}")

# 替换为您的钉钉群组的Webhook地址

print(webhook_url)
# 替换为您要发送的消息内容
message_content = "Hello, 1111这是一条测试消息！"
#
# # 发送消息
send_dingding_message(webhook_url, message_content)